// 生成基本类型的slice去除重复元素的函数

// +build ignore
// Generate with: go run gen.go -output remove_duplicate.go

package main

import (
	"bytes"
	"flag"
	"io/ioutil"
	"log"
	"strings"
	"text/template"
)

var types = []string{
	"int",
	"int8",
	"int16",
	"int32",
	"int64",
	"uint",
	"uint8",
	"uint16",
	"uint32",
	"uint64",
	"float32",
	"float64",
	"string",
}

var filename = flag.String("output", "remove_duplicate.go", "output file name")

func main() {
	flag.Parse()

	tmpl := `// Do not edit this file.
// Generate with: go run gen.go -output remove_duplicate.go

package tools
{{range $Type := .}} 
// RemoveDuplicate{{$Type|title}} 删除slice中重复的元素
func RemoveDuplicate{{$Type|title}} (a []{{$Type}}) []{{$Type}} {
	res := make([]{{$Type}}, 0)
TOP:
	for i := 0; i < len(a); i++ {
		for j := 0; j < len(res); j++ {
			if a[i] == res[j] {
				continue TOP
			}
		}
		res = append(res, a[i])
	}
	return res
}{{end}}
`
	funcs := template.FuncMap{
		"title": strings.Title,
	}
	t := template.Must(template.New("rmduplicate").Funcs(funcs).Parse(tmpl))
	var buf bytes.Buffer
	err := t.Execute(&buf, types)
	if err != nil {
		log.Fatal(err)
	}
	err = ioutil.WriteFile(*filename, buf.Bytes(), 0644)
	if err != nil {
		log.Fatal(err)
	}
}
